package com.ai.doc.util;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Map;

public class DocUtil {
    public static void createDoc(String ftlName, Map<String, Object> dataMap, String docFilePath) throws IOException {
        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");

        configuration.setClassForTemplateLoading(DocUtil.class, "/");
        Template t = configuration.getTemplate(ftlName);

        File docFile = new File(docFilePath);

        try (Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile), StandardCharsets.UTF_8))) {
            t.process(dataMap, out);
        } catch (TemplateException | IOException e) {
            e.printStackTrace();
        }
    }
}
